package biblioteca.interfaces.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import biblioteca.interfaces.IConexao;


public class GenericDao implements IConexao {	
	
	private Connection conexao;	
	
	public GenericDao(){
		iniciaConexao();
	}	
	
	private void iniciaConexao() {
		try {
			Class.forName(NOME_DRIVER);	
		} catch (ClassNotFoundException e) {		
			e.printStackTrace();
		}
	}
	
	
	@Override
	public Connection getConexao() {		
		try {
			if(this.conexao == null)			
				this.conexao = DriverManager.getConnection("jdbc:mysql:"+SERVIDOR_DB+NOME_DB+NOME_USUARIO_DB+SENHA_USUARIO_DB);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conexao;		
	}	
	

	@Override
	public void fechaConexao() {
		try {
			this.conexao.close();
		} catch (SQLException e) {		
			e.printStackTrace();
		}		
	}
	
	

}
